package com.abel.bigwater.dataserver.model.ewide

import com.abel.bigwater.data.BcdString
import io.netty.buffer.Unpooled
import junit.framework.Assert.*
import org.junit.Assert
import org.junit.Test

class DeviceStatusTest {

    @Test
    fun parseBuf() {
        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray("01 00 00"))
        val ds = DeviceStatus()
        assertTrue(ds.parseBuf(buf))
        Assert.assertTrue(ds.lowBattery10)
        assertFalse(ds.fault48511 || ds.devMoved17)
        assertFalse(ds.breakLine20 || ds.magnet21 || ds.exceedHigh22 || ds.exceedLow23 || ds.readMutant24)
        assertFalse(ds.breakLine30 || ds.magnet31 || ds.exceedHigh32 || ds.exceedLow33 || ds.readMutant34)

        buf.clear().writeBytes(BcdString.toByteArray("FF FF FF"))
        val ds2 = DeviceStatus()
        assertTrue(ds2.parseBuf(buf))
        Assert.assertTrue(ds2.lowBattery10)
        assertTrue(ds2.fault48511 && ds2.devMoved17)
        assertTrue(ds2.breakLine20 && ds2.magnet21 && ds2.exceedHigh22 && ds2.exceedLow23 && ds2.readMutant24)
        assertTrue(ds2.breakLine30 && ds2.magnet31 && ds2.exceedHigh32 && ds2.exceedLow33 && ds2.readMutant34)
    }

    @Test
    fun pack() {
        val ds = DeviceStatus().apply {
            devMoved17 = true
        }
        val buf = Unpooled.buffer()
        ds.pack(buf)
        assertEquals(3, buf.readableBytes())
        Assert.assertArrayEquals(byteArrayOf(0x80.toByte(), 0, 0), ByteArray(3).also { buf.readBytes(it) })

        ds.apply {
            breakLine20 = true
            breakLine30 = true
        }
        buf.clear().also { ds.pack(it) }
        assertEquals(3, buf.readableBytes())
        Assert.assertArrayEquals(byteArrayOf(0x80.toByte(), 1, 1), ByteArray(3).also { buf.readBytes(it) })
    }
}